/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */

/*
 * ExpressionSelectionPanel.java
 *
 * Created on 27-May-2011, 17:09:30
 */
package acid.plugin.gui;

import acid.plugin.SetVisualStyle;
import acid.plugin.expression.ShowExpressionCorrelations;
import acid.plugin.expression.ShowExpressionData;
import acid.spring.OrganismService;
import acid.spring.db.Organism;
import cytoscape.Cytoscape;
import cytoscape.task.Task;
import cytoscape.task.ui.JTaskConfig;
import cytoscape.task.util.TaskManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SpinnerNumberModel;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author morgan
 */
public class ExpressionSelectionPanel extends javax.swing.JPanel
        implements ActionListener {

    private OrganismService organismService = null;
    private String separateBy = "None";

    /** Creates new form ExpressionSelectionPanel */
    public ExpressionSelectionPanel() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:/jdbc-services.xml");
        BeanFactory factory = context;
        organismService = (OrganismService) factory.getBean(OrganismService.class);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel5 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        organismNameVarBox = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        pValSlider = new javax.swing.JSpinner();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        foldChangeSlider = new javax.swing.JSpinner();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();
        tissueRadioButton = new javax.swing.JRadioButton();
        experimentRadioButton = new javax.swing.JRadioButton();
        noneRadioButton = new javax.swing.JRadioButton();
        jSeparator3 = new javax.swing.JSeparator();
        coExpressionBox = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Overlay expression data on network"));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel5.setMaximumSize(new java.awt.Dimension(32822, 30000));
        jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel2.setBorder(null);
        jPanel2.setAlignmentX(0.0F);
        jPanel2.setAlignmentY(1.0F);
        jPanel2.setMaximumSize(new java.awt.Dimension(32818, 30));
        jPanel2.setMinimumSize(new java.awt.Dimension(131, 30));
        jPanel2.setPreferredSize(new java.awt.Dimension(500, 40));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jLabel2.setText("Organism: ");
        jLabel2.setAlignmentX(1.0F);
        jLabel2.setMaximumSize(new java.awt.Dimension(155, 18));
        jLabel2.setMinimumSize(new java.awt.Dimension(155, 18));
        jLabel2.setPreferredSize(new java.awt.Dimension(155, 18));
        jPanel2.add(jLabel2);

        Object[] organisms = new String[]{""};
        if (organismService != null)
        {
            organisms = organismService.listAllOrganisms().toArray();
        }
        organismNameVarBox.setModel(new javax.swing.DefaultComboBoxModel(organisms));
        organismNameVarBox.setAlignmentX(1.0F);
        organismNameVarBox.setMaximumSize(new java.awt.Dimension(32767, 28));
        jPanel2.add(organismNameVarBox);

        jPanel5.add(jPanel2);

        jSeparator2.setMaximumSize(new java.awt.Dimension(32767, 10));
        jPanel5.add(jSeparator2);

        jLabel4.setText("Only use expression data with: ");
        jLabel4.setAlignmentY(1.0F);
        jLabel4.setAutoscrolls(true);
        jLabel4.setOpaque(true);
        jPanel5.add(jLabel4);

        jPanel3.setBorder(null);
        jPanel3.setAlignmentX(0.0F);
        jPanel3.setAlignmentY(1.0F);
        jPanel3.setMaximumSize(new java.awt.Dimension(32818, 30));
        jPanel3.setMinimumSize(new java.awt.Dimension(131, 30));
        jPanel3.setPreferredSize(new java.awt.Dimension(500, 40));
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        jLabel8.setText("         ");
        jPanel3.add(jLabel8);

        jLabel5.setText("P value at least:");
        jLabel5.setMaximumSize(new java.awt.Dimension(156, 18));
        jLabel5.setMinimumSize(new java.awt.Dimension(156, 18));
        jLabel5.setPreferredSize(new java.awt.Dimension(156, 18));
        jPanel3.add(jLabel5);

        pValSlider.setModel(new javax.swing.SpinnerNumberModel(0.05d, 0.0d, 0.05d, 0.001d));
        pValSlider.setMaximumSize(new java.awt.Dimension(32767, 28));
        jPanel3.add(pValSlider);

        jPanel5.add(jPanel3);

        jPanel4.setBorder(null);
        jPanel4.setAlignmentX(0.0F);
        jPanel4.setAlignmentY(1.0F);
        jPanel4.setMaximumSize(new java.awt.Dimension(32818, 30));
        jPanel4.setMinimumSize(new java.awt.Dimension(131, 30));
        jPanel4.setPreferredSize(new java.awt.Dimension(500, 30));
        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.LINE_AXIS));

        jLabel9.setText("         ");
        jPanel4.add(jLabel9);

        jLabel6.setText("Fold change at least:");
        jLabel6.setMaximumSize(new java.awt.Dimension(156, 18));
        jLabel6.setMinimumSize(new java.awt.Dimension(156, 18));
        jLabel6.setPreferredSize(new java.awt.Dimension(156, 18));
        jPanel4.add(jLabel6);

        foldChangeSlider.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.5d), Double.valueOf(0.5d), null, Double.valueOf(0.1d)));
        foldChangeSlider.setMaximumSize(new java.awt.Dimension(32767, 28));
        jPanel4.add(foldChangeSlider);

        jPanel5.add(jPanel4);

        jSeparator1.setMaximumSize(new java.awt.Dimension(32767, 10));
        jPanel5.add(jSeparator1);

        jLabel7.setText("Separate expression data by: ");
        jLabel7.setAlignmentY(1.0F);
        jPanel5.add(jLabel7);

        buttonGroup1.add(tissueRadioButton);
        tissueRadioButton.setText("Tissue");
        tissueRadioButton.setAlignmentY(1.0F);
        tissueRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tissueRadioButtonActionPerformed(evt);
            }
        });
        jPanel5.add(tissueRadioButton);

        buttonGroup1.add(experimentRadioButton);
        experimentRadioButton.setText("Experiment");
        experimentRadioButton.setAlignmentY(1.0F);
        experimentRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                experimentRadioButtonActionPerformed(evt);
            }
        });
        jPanel5.add(experimentRadioButton);

        buttonGroup1.add(noneRadioButton);
        noneRadioButton.setSelected(true);
        noneRadioButton.setText("None (show all)");
        noneRadioButton.setAlignmentY(1.0F);
        noneRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noneRadioButtonActionPerformed(evt);
            }
        });
        jPanel5.add(noneRadioButton);
        jPanel5.add(jSeparator3);

        coExpressionBox.setSelected(true);
        coExpressionBox.setText("Calculate co-expression values");
        jPanel5.add(coExpressionBox);

        add(jPanel5);
    }// </editor-fold>//GEN-END:initComponents

    private void tissueRadioButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_tissueRadioButtonActionPerformed
    {//GEN-HEADEREND:event_tissueRadioButtonActionPerformed
        separateBy = ((JRadioButton) evt.getSource()).getText();
    }//GEN-LAST:event_tissueRadioButtonActionPerformed

    private void experimentRadioButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_experimentRadioButtonActionPerformed
    {//GEN-HEADEREND:event_experimentRadioButtonActionPerformed
        separateBy = ((JRadioButton) evt.getSource()).getText();
    }//GEN-LAST:event_experimentRadioButtonActionPerformed

    private void noneRadioButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_noneRadioButtonActionPerformed
    {//GEN-HEADEREND:event_noneRadioButtonActionPerformed
        separateBy = ((JRadioButton) evt.getSource()).getText();
    }//GEN-LAST:event_noneRadioButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox coExpressionBox;
    private javax.swing.JRadioButton experimentRadioButton;
    private javax.swing.JSpinner foldChangeSlider;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JRadioButton noneRadioButton;
    private javax.swing.JComboBox organismNameVarBox;
    private javax.swing.JSpinner pValSlider;
    private javax.swing.JRadioButton tissueRadioButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = JOptionPane.showOptionDialog(null, this, "Overlay Expression Data",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
                null, null);
        if (i != JOptionPane.OK_OPTION) {
            return;
        }

        double maxPVal = ((SpinnerNumberModel) pValSlider.getModel()).getNumber().doubleValue();
        double minFoldChange = ((SpinnerNumberModel) foldChangeSlider.getModel()).getNumber().doubleValue();
        System.out.println("pval = " + maxPVal + " foldChange = " + minFoldChange);

        Organism organism = (Organism) organismNameVarBox.getSelectedItem();
        ShowExpressionData sed = new ShowExpressionData(maxPVal, minFoldChange,
                organism);
        SetVisualStyle svs = new SetVisualStyle();
        if (separateBy.equals("Tissue")) {
            TissueSelectionPanel pane = new TissueSelectionPanel();
            int j = JOptionPane.showOptionDialog(this, pane,
                    "Choose tissues to display", JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE, null, null, null);
            if (j == JOptionPane.OK_OPTION) {
                sed.setTissues(pane.getSelectedTissues());
                executeTask(sed);
                executeTask(svs);
            }
        } else if (separateBy.equals("Experiment")) {
            ExperimentSelectionPanel pane = new ExperimentSelectionPanel();
            int j = JOptionPane.showOptionDialog(this, pane,
                    "Choose experiments to display", JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE, null, null, null);
            if (j == JOptionPane.OK_OPTION) {
                sed.setExperiments(pane.getSelectedExperiments());
                executeTask(sed);
                executeTask(svs);
            }
        } else {
            executeTask(sed);
            if (coExpressionBox.isSelected()) {
                ShowExpressionCorrelations sec = new ShowExpressionCorrelations(sed.getRed(),
                        maxPVal, minFoldChange);
                executeTask(sec);
            }
            executeTask(svs);
        }
        AcidResultsPanel.getInstance().showLegend(true);

    }

    private void executeTask(Task task) {
        // Configure JTask Dialog Pop-Up Box
        JTaskConfig jTaskConfig = new JTaskConfig();
        jTaskConfig.setOwner(Cytoscape.getDesktop());
        jTaskConfig.displayCloseButton(true);

        jTaskConfig.displayCancelButton(false);

        jTaskConfig.displayStatus(true);
        jTaskConfig.setAutoDispose(true);

        // Execute Task in New Thread; pops open JTask Dialog Box.
        TaskManager.executeTask(task, jTaskConfig);
    }
}
